﻿using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using Agility.Testing.Silverlight.AppDriver;
using Agility.Web.Models.DomainModel;
using Microsoft.Silverlight.Testing;
using NUnit.Framework;
using System;

namespace Agility.Testing.Silverlight.ViewModels
{
    [TestFixture]
    public class MyIdeasViewModelTests : SilverlightTest
    {
        RequirementsDriver requirementsDriver;
        AdministrationDriver adminDriver;
        List<Entity> myIdeas;

        //[TestFixtureSetUp]
        //public void SetupTestFixture()
        //{
        //}

        //[TestFixtureTearDown]
        //public void TeardownTestFixture()
        //{
        //}

        [SetUp]
        public void SetupTest()
        {
            this.requirementsDriver = new RequirementsDriver();
            this.myIdeas = new List<Entity>();
            Epic e1 = new Epic();
            e1.Description = "This is the first epic idea";
            e1.Title = "First Epic Idea";
            this.myIdeas.Add(e1);
            Epic e2 = new Epic();
            e2.Description = "This is the second epic idea";
            e2.Title = "Second Epic Idea";
            this.myIdeas.Add(e2);
            Epic e3 = new Epic();
            e3.Description = "This is the third epic idea";
            e3.Title = "Third Epic Idea";
            this.myIdeas.Add(e3);

            this.requirementsDriver.SetupTestData(myIdeas);
        }

        [TearDown]
        public void TeardownTest()
        {
            this.myIdeas.Clear();
            this.requirementsDriver.Cleanup();
            this.requirementsDriver = null;
            this.adminDriver = null;
        }

        #region Unittests

        [Test]
        [Category("UnitTest")]
        [Asynchronous]
        public void TestConstructor()
        {
            // Act
            this.EnqueueCallback(() => this.requirementsDriver.CreateMyIdeasViewModel());

            // Assert
            this.EnqueueCallback(() => Assert.IsTrue(this.requirementsDriver.VerifyNothingDisplayed()));
            this.EnqueueTestComplete();
        }


        [Test]
        [Category("UnitTest")]
        [Asynchronous]
        public void TestAddIdea()
        {
            // Arrange
            this.adminDriver = new AdministrationDriver(TestType.UnitTest);
            
            // Act
            this.EnqueueCallback(() => this.requirementsDriver.CreateMyIdeasViewModel());
            this.EnqueueCallback(() => this.adminDriver.Login("Dave"));
            this.EnqueueConditional(() => this.requirementsDriver.MyIdeasUpdated == true);
            this.EnqueueCallback(() => this.requirementsDriver.AddNewIdea());

            // Assert
            this.EnqueueCallback(() => Assert.AreEqual("Success", this.requirementsDriver.VerifyMyIdeas()));
            this.EnqueueCallback(() => this.adminDriver.Logout());
            this.EnqueueTestComplete();
        }

        // TODO: Implement these tests
        [Test]
        [Category("UnitTest")]
        [Asynchronous]
        [Ignore("Implementation pending")]
        public void TestUpdateIdea()
        {
            // Arrange

            // Act
            this.EnqueueCallback(() => this.adminDriver.Login("Dave"));
            this.EnqueueCallback(() => this.requirementsDriver.UpdateIdea());

            // Assert
            this.EnqueueCallback(() => Assert.AreEqual("Success", this.requirementsDriver.VerifyMyIdeas()));
            this.EnqueueCallback(() => this.adminDriver.Logout());
            this.EnqueueTestComplete();
        }

        [Test]
        [Category("UnitTest")]
        [Asynchronous]
        [Ignore("Implementation pending")]
        public void TestDeleteIdea()
        {
            // Arrange

            // Act
            this.EnqueueCallback(() => this.adminDriver.Login("Dave"));
            this.EnqueueCallback(() => this.requirementsDriver.DeleteIdea());

            // Assert
            this.EnqueueCallback(() => Assert.AreEqual("Success", this.requirementsDriver.VerifyMyIdeas()));
            this.EnqueueCallback(() => this.adminDriver.Logout());
            this.EnqueueTestComplete();
        }

        #endregion

        #region Componenttests

        [Test]
        [Category("ComponentTest")]
        [Asynchronous]
        public void TestAuthorization()
        {
            // Arrange
            this.adminDriver = new AdministrationDriver(TestType.ComponentTest);

            // Act
            this.EnqueueCallback(() => this.requirementsDriver.CreateMyIdeasViewModel());
            this.EnqueueCallback(() => this.adminDriver.Login("Dave"));

            // Assert
            this.EnqueueConditional(() => this.requirementsDriver.MyIdeasUpdated == true);
            this.EnqueueCallback(() => Assert.AreEqual("Success", this.requirementsDriver.VerifyMyIdeas()));
            this.EnqueueCallback(() => this.adminDriver.Logout());
            this.EnqueueTestComplete();
        }

        #endregion

        
    }
}
